<?php
session_start();
include $_SERVER['DOCUMENT_ROOT'].'/application/config/Database.php';

class Betting_lib {
	
	function __construct(){
		
	}
	
	function get_products(){
		$con = new Database();
		$result = $con->get(array('table'=>'products','fields'=>array('product_id as id','name')));
		
		return $result;
	}
	function get_users(){
		$con = new Database();
		$result = $con->get(array('table'=>'users','fields'=>array('user_id','username')));

		return $result;
	}
	function get_bids(){
		$con = new Database();
		$result = $con->get_query('Select p.product_id,p.name,u.username,s.sum from products p left outer join bid_sum s on p.product_id = s.product_id left join users u on u.user_id = s.user_id');
		return $result;
	}
	
	function get_userid($username,$return_type = false){
		$con = new Database();
		$result = $con->get_query('SELECT user_id FROM users WHERE username="'.$username.'"');
		
		if($return_type == 'value'){
			$row = $result->fetch_object();
			return $row->user_id;
		} else {
			return $result;
		}
	}
	function new_wallet($userid,$username, $time){
		$con = new Database();
		$result = $con->new_wallet($userid, $username, $time);
		return $result;
	}
	function get_wallet($userid){
		$con = new Database();
		//$wallet = $con->get_query('Select w.deposit from users u left join users_wallet w on u.user_id = w.user_id where u.userid = "'.$userid.'"');
		$wallet = $con->get(array('table'=>'users_wallet','fields'=>array('deposit'),'where'=>array('user_id'=>$userid)));
		
		while($row = $wallet->fetch_object()){
			$amount = $row->deposit;
		}
		
		return $amount;
	}
	function topup_wallet($userid, $amount) {
		$con = new Database();
		$con -> update_wallet($userid,$amount);
	}
	function delete_bid($id){
		$con = new Database();
		$con->delete('bids', array('id'=>12));
	}
	
	function insert_bid($product_id){
		$con = new Database();
		return $con->update($product_id,$_SESSION['local_userid']);
		//$con->close_connection($con);
	}
	function insert_test_bid($product_id,$user_id){
		$con = new Database();
		echo $con->update($product_id,$user_id);
		//$con->close_connection($con);
	}
}



?>